package com.huangcy.gulimall.member.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huangcy.common.utils.HttpUtils;
import com.huangcy.gulimall.member.dao.MemberLevelDao;
import com.huangcy.gulimall.member.entity.MemberLevelEntity;
import com.huangcy.gulimall.member.exception.PhoneExistException;
import com.huangcy.gulimall.member.exception.UserNameExistException;
import com.huangcy.gulimall.member.vo.MemberLoginVo;
import com.huangcy.gulimall.member.vo.MemberRegisterVo;
import com.huangcy.gulimall.member.vo.SocialUserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huangcy.common.utils.PageUtils;
import com.huangcy.common.utils.Query;

import com.huangcy.gulimall.member.dao.MemberDao;
import com.huangcy.gulimall.member.entity.MemberEntity;
import com.huangcy.gulimall.member.service.MemberService;
import org.springframework.transaction.annotation.Transactional;


@Service("memberService")
@Slf4j
public class MemberServiceImpl extends ServiceImpl<MemberDao, MemberEntity> implements MemberService {

    @Autowired
    MemberLevelDao memberLevelDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemberEntity> page = this.page(
                new Query<MemberEntity>().getPage(params),
                new QueryWrapper<MemberEntity>()
        );

        return new PageUtils(page);
    }

    @Transactional
    @Override
    public void register(MemberRegisterVo vo) {
        MemberEntity entity = new MemberEntity();

        // 设置默认等级
        MemberLevelEntity levelEntity = memberLevelDao.selectOne(new QueryWrapper<MemberLevelEntity>().eq("default_status", 1));
        entity.setLevelId(levelEntity.getId());

        // 检查用户名和手机号是否唯一 为了让Controller能感知异常，异常机制
        checkPhoneUnique(vo.getPhone());
        checkUserNameUnique(vo.getUserName());

        entity.setMobile(vo.getPhone());
        entity.setUsername(vo.getUserName());
        entity.setNickname(vo.getUserName());

        // 密码要进行加密存储 加盐：$1$+8位字符
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(vo.getPassword());
        entity.setPassword(encode);


        // 存入数据库
        baseMapper.insert(entity);
    }

    @Override
    public void checkPhoneUnique(String phone) throws PhoneExistException {
        Integer mobile = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (mobile > 0) {
            throw new PhoneExistException();
        }
    }

    @Override
    public void checkUserNameUnique(String userName) throws UserNameExistException {
        Integer username = baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));
        if (username > 0) {
            throw new UserNameExistException();
        }
    }

    @Override
    public MemberEntity login(MemberLoginVo vo) {
        String loginacct = vo.getLoginacct();
        String password = vo.getPassword();

        // 去数据库查询
        QueryWrapper<MemberEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("username", loginacct).or().eq("mobile", loginacct);
        MemberEntity entity = baseMapper.selectOne(wrapper);
        if (entity == null) {
            return null;
        } else {
            // 获取到数据库的password
            String passwordDb = entity.getPassword();
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

            // 进行密码匹配
            boolean matches = passwordEncoder.matches(password, passwordDb);
            if (matches) {
                return entity;
            } else {
                return null;
            }
        }

    }

    @Override
    public MemberEntity login(SocialUserVo socialUser) throws Exception {
        Map<String, String> query = new HashMap<>();
        query.put("access_token", socialUser.getAccessToken());
        HttpResponse response = HttpUtils.doGet("https://gitee.com", "/api/v5/user", "get", new HashMap<String, String>(), query);
        String socialUid = null;
        MemberEntity register = new MemberEntity();
        if (response.getStatusLine().getStatusCode() == 200) {
            String json = EntityUtils.toString(response.getEntity());
            JSONObject jsonObject = JSON.parseObject(json);
            socialUid = jsonObject.getString("id");
            log.info(socialUid);
            socialUser.setSocialUid(socialUid);
            // 昵称
            String name = jsonObject.getString("name");
            register.setNickname(name);
        }
        // 具有登录和注册合并逻辑
        MemberEntity memberEntity = baseMapper.selectOne(new QueryWrapper<MemberEntity>().eq("social_uid", socialUid));
        if (memberEntity != null) {
            // 这个用户已经注册了
            MemberEntity update = new MemberEntity();
            update.setId(memberEntity.getId());
            update.setAccessToken(socialUser.getAccessToken());
            baseMapper.updateById(update);
            memberEntity.setAccessToken(socialUser.getAccessToken());
            memberEntity.setExpiresIn(socialUser.getExpiresIn());
            return memberEntity;
        }
        register.setSocialUid(socialUser.getSocialUid());
        register.setAccessToken(socialUser.getAccessToken());
        register.setExpiresIn(socialUser.getExpiresIn());
        baseMapper.insert(register);
        return register;
    }
}
